package io.renren.modules.sys.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.service.entity.WorkerEntity;
import io.renren.modules.service.service.WorkerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author chenzh
 * @email ${email}
 * @date 2019-05-06 22:29:58
 */
@RestController
@RequestMapping("sys/worker")
public class SysWorkerController {
    @Autowired
    private WorkerService workerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:worker:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = workerService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/select")
    @RequiresPermissions("sys:worker:select")
    public R select() {
        List<WorkerEntity> list = workerService.list();
        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:worker:info")
    public R info(@PathVariable("id") Integer id) {
        WorkerEntity worker = workerService.getById(id);

        return R.ok().put("worker", worker);
    }

    /**
     * 保存
     */
    @SysLog("保存工位")
    @RequestMapping("/save")
    @RequiresPermissions("sys:worker:save")
    public R save(@RequestBody WorkerEntity worker) {
        ValidatorUtils.validateEntity(worker, AddGroup.class);
        worker.setCreateTime(new Date());
        workerService.save(worker);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改工位")
    @RequestMapping("/update")
    @RequiresPermissions("sys:worker:update")
    public R update(@RequestBody WorkerEntity worker) {
        ValidatorUtils.validateEntity(worker, UpdateGroup.class);
        workerService.updateById(worker);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除工位")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:worker:delete")
    public R delete(@RequestBody Integer[] ids) {
        workerService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
